--悦容藏锋
local yuerongcangfeng = fk.CreateSkill {
    name = "mcdh_yuerongcangfeng",
    on_acquire = function(self, player)
        local room = player.room
        local ids = {}
        room.logic:getEventsOfScope(GameEvent.MoveCards, 999, function(e)
            for _, move in ipairs(e.data) do
                if move.toArea == Card.DiscardPile then
                    for _, info in ipairs(move.moveInfo) do
                        if room:getCardArea(info.cardId) == Card.DiscardPile and Fk:getCardById(info.cardId).color == Card.Black then
                            table.insertIfNeed(ids, info.cardId)
                        end
                    end
                end
            end
        end, Player.HistoryTurn)
        room:setPlayerMark(player, "mcdh_yuerongcangfeng_card-turn", ids)
        room:setPlayerMark(player, "@mcdh_yuerongcangfeng_count-turn", #ids)
    end
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

yuerongcangfeng:addEffect(fk.Damaged,{
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(self) and target == player
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local num = player:getMark("@mcdh_yuerongcangfeng_count-turn")
        local cards = room:askForDiscard(player, 1, 999,true, self.name, true,".|.|heart,diamond","#mcdh_yuerongcangfeng:::"..num)
        if #cards > 0 then
            self.cost_data = {#cards}
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local num = player:getMark("@mcdh_yuerongcangfeng_count-turn")
        if num <= self.cost_data[1] then
            U.askForUseVirtualCard(room, player, "slash", nil, self.name, "#mcdh_yuerongcangfeng_use", false, true, false, false, nil, false)
        end
        if num >= self.cost_data[1] then
            local card = Fk:cloneCard("ex_nihilo")
            card.skillName = self.name
            if not player:isProhibited(player, card) then
                local use = {
                    from = player.id,
                    tos = table.map({room.current.id}, function(p) return {p} end),
                    card = card,
                }
                room:useCard(use)
            end
        end
    end,
})

yuerongcangfeng:addEffect(fk.EventPhaseStart,{
    can_trigger = function(self, event, target, player, data)
        if event == fk.Damaged then
            return player:hasSkill(self) and target == player
        else
            return target == player and player:hasSkill(self) and player.phase == Player.Finish
        end
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local num = player:getMark("@mcdh_yuerongcangfeng_count-turn")
        local cards = room:askForDiscard(player, 1, 999,true, self.name, true,".|.|heart,diamond","#mcdh_yuerongcangfeng:::"..num)
        if #cards > 0 then
            self.cost_data = {#cards}
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local num = player:getMark("@mcdh_yuerongcangfeng_count-turn")
        if num <= self.cost_data[1] then
            U.askForUseVirtualCard(room, player, "slash", nil, self.name, "#mcdh_yuerongcangfeng_use", false, true, false, false, nil, false)
        end
        if num >= self.cost_data[1] then
            local card = Fk:cloneCard("ex_nihilo")
            card.skillName = self.name
            if not player:isProhibited(player, card) then
                local use = {
                    from = player.id,
                    tos = table.map({room.current.id}, function(p) return {p} end),
                    card = card,
                }
                room:useCard(use)
            end
        end
    end
})

yuerongcangfeng:addEffect(fk.AfterCardsMove,{
    can_refresh = function(self, event, target, player, data)
        if player:hasSkill(self, true) then
                for _, move in ipairs(data) do
                    if move.toArea == Card.DiscardPile then
                        for _, info in ipairs(move.moveInfo) do
                            if player.room:getCardArea(info.cardId) == Card.DiscardPile then
                                return true
                            end
                        end
                    end
                    for _, info in ipairs(move.moveInfo) do
                        if info.fromArea == Card.DiscardPile and player.room:getCardArea(info.cardId) ~= Card.DiscardPile  then
                            return true
                        end
                    end
                end
        end
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local ids =  player:getTableMark("mcdh_yuerongcangfeng_card-turn")
            for _, move in ipairs(data) do
                if move.toArea == Card.DiscardPile then
                    for _, info in ipairs(move.moveInfo) do
                        if room:getCardArea(info.cardId) == Card.DiscardPile and Fk:getCardById(info.cardId).color == Card.Black then
                            table.insertIfNeed(ids, info.cardId)
                        end
                    end
                end
                for _, info in ipairs(move.moveInfo) do
                    if info.fromArea == Card.DiscardPile and room:getCardArea(info.cardId) ~= Card.DiscardPile then
                        table.removeOne(ids, info.cardId)
                    end
                end
            end
        room:setPlayerMark(player, "mcdh_yuerongcangfeng_card-turn", ids)
        room:setPlayerMark(player, "@mcdh_yuerongcangfeng_count-turn", #ids)
    end,
})

Fk:loadTranslationTable {
    ["@mcdh_yuerongcangfeng_count-turn"]="悦容藏锋",
    ["mcdh_yuerongcangfeng"] = "悦容藏锋",
    [":mcdh_yuerongcangfeng"] = "『悦容藏锋』<br>结束阶段或当你受到伤害后，你可以弃置至少一张红色牌，若你弃置牌数：不小于X，你视为使用一张【杀】；不大于X，你视为对当前回合角色使用一张【无中生有】。（X为弃牌堆中本回合进入的黑色牌数）",
    ["#mcdh_yuerongcangfeng"] = "悦容藏锋:弃置至少一张红色牌（目前弃牌堆中本回合进入的黑色牌数为 %arg ）",
    ["#mcdh_yuerongcangfeng_use"] ="悦容藏锋:视为使用一张杀"
}

return yuerongcangfeng